package com.pany.rememberpass;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * @name： RememberPass
 * @author： zs
 * @time： 2022/5/13 0013
 */
public class NetUtils {

    public static String doGet() {
        String resultJSONString = "";
        BufferedReader bufferedReader = null;
        try {
            // 1.建立链接
            HttpURLConnection httpURLConnection = null;
            String rul = "https://www.baidu.com/";
            URL requestRrl = new URL(rul);
            httpURLConnection = (HttpURLConnection) requestRrl.openConnection();
            httpURLConnection.setRequestMethod("GET");
            httpURLConnection.setConnectTimeout(5000);
            httpURLConnection.connect();

            //.2.获取二进制流
            InputStream inputStream = httpURLConnection.getInputStream();
            //3.将二进制流 包装
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            bufferedReader = new BufferedReader(inputStreamReader);
            // 4.从 bufferread中读取字符串
            String line = "";
            StringBuffer stringBuffer = new StringBuffer();

            while ((line = bufferedReader.readLine()) != null) {
                stringBuffer.append(line);
                stringBuffer.append("\n");
            }
            if (stringBuffer.length() == 0) {
                return null;
            }
            resultJSONString = stringBuffer.toString();

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return resultJSONString;
    }
}
